package cn.community.system.web.controller;

import cn.community.system.domain.DeviceRecord;
import cn.community.system.query.DeviceRecordQuery;
import cn.community.system.query.QueryObject;
import cn.community.system.service.IDeviceRecordService;
import cn.community.system.util.JsonResult;
import cn.community.system.vo.ApplyLendVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/deviceRecord")
public class DeviceRecordController {
    @Autowired
    private IDeviceRecordService deviceRecordService;

    @RequestMapping("/admin/list")
    @RequiresPermissions(value = {"deviceRecord/admin/list","超管-设备借还列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") DeviceRecordQuery qo){
        model.addAttribute("page",deviceRecordService.query(qo));
        return "device/admin/list";
    }

    @RequestMapping("/saveLend")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/saveLend","超管-设备借用申请"},logical = Logical.OR)
    public Object saveLend(ApplyLendVo applyLendVo){
        deviceRecordService.saveLend(applyLendVo);
        return JsonResult.success();
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"deviceRecord/input","超管-设备借还记录新增/编辑页面"},logical = Logical.OR)
    public String input(Model model,Long id){
        if(id!=null){ //编辑
            DeviceRecord deviceRecord = deviceRecordService.get(id);
            model.addAttribute("deviceRecord", deviceRecord);
        }
        return "device/admin/input";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/saveOrUpdate","超管-设备借还记录新增/编辑功能"},logical = Logical.OR)
    public Object saveOrUpdate(DeviceRecord deviceRecord){
        if(deviceRecord.getId() == null){ // 代表新增
            deviceRecordService.save(deviceRecord);
        }else { // 代表修改
            deviceRecordService.update(deviceRecord);
        }
        return JsonResult.success();
    }

    @RequestMapping("/admin/leadList")
    @RequiresPermissions(value = {"deviceRecord/admin/leadList","超管-设备借用历史"},logical = Logical.OR)
    public String leadList(Model model, @ModelAttribute("qo") DeviceRecordQuery qo){
        model.addAttribute("page",deviceRecordService.queryLeadList(qo));
        return "device/admin/lendList";
    }

    @RequestMapping("/admin/returnList")
    @RequiresPermissions(value = {"deviceRecord/admin/returnList","超管-设备归还历史"},logical = Logical.OR)
    public String returnList(Model model, @ModelAttribute("qo") DeviceRecordQuery qo){
        model.addAttribute("page",deviceRecordService.queryReturnList(qo));
        return "device/admin/returnList";
    }

    @RequestMapping("/association/list")
    @RequiresPermissions(value = {"deviceRecord/association/list","社团-设备借还列表"},logical = Logical.OR)
    public String listAsso(Model model, @ModelAttribute("qo") DeviceRecordQuery qo){
        model.addAttribute("page",deviceRecordService.queryAsso(qo));
        return "device/association/list";
    }

    @RequestMapping("/saveLendAsso")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/saveLendAsso","社团-设备借用申请"},logical = Logical.OR)
    public Object saveLendAsso(ApplyLendVo applyLendVo){
        deviceRecordService.saveLendAsso(applyLendVo);
        return JsonResult.success();
    }

    @RequestMapping("/association/leadList")
    @RequiresPermissions(value = {"deviceRecord/association/leadList","社团-设备借用历史"},logical = Logical.OR)
    public String leadListAsso(Model model, @ModelAttribute("qo") DeviceRecordQuery qo){
        model.addAttribute("page",deviceRecordService.queryLeadListAsso(qo));
        return "device/association/lendList";
    }

    @RequestMapping("/association/returnList")
    @RequiresPermissions(value = {"deviceRecord/association/returnList","社团-设备归还历史"},logical = Logical.OR)
    public String returnListAsso(Model model, @ModelAttribute("qo") DeviceRecordQuery qo){
        model.addAttribute("page",deviceRecordService.queryReturnListAsso(qo));
        return "device/association/returnList";
    }

    // 申请归还
    @RequestMapping("/applyReturn")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/applyReturn","申请归还设备"},logical = Logical.OR)
    public Object applyReturn(Integer hot, Long id){
        deviceRecordService.applyReturn(hot,id);
        return JsonResult.success();
    }

    @RequestMapping("/inputAsso")
    public String inputAsso(Model model,Long id){
        if(id!=null){ //编辑
            DeviceRecord deviceRecord = deviceRecordService.get(id);
            model.addAttribute("device", deviceRecord);
        }
        return "device/association/input";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/delete","设备借用记录删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        deviceRecordService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/agreeReturn")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/agreeReturn","设备归还审核"},logical = Logical.OR)
    public JsonResult agreeReturn(DeviceRecord deviceRecord){
        deviceRecordService.agreeReturn(deviceRecord);
        return JsonResult.success();
    }

    @RequestMapping("/agreeLeading")
    @ResponseBody
    @RequiresPermissions(value = {"deviceRecord/agreeLeading","设备借用审核"},logical = Logical.OR)
    public JsonResult agreeLeading(DeviceRecord deviceRecord){
        deviceRecordService.agreeLeading(deviceRecord);
        return JsonResult.success();
    }

}
